package com.yonyou.pmclouds.completedcatalog.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.entity.PageObject;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.completedcatalog.entity.CompletedCatalogBVO;
import com.yonyou.pmclouds.completedcatalog.entity.CompletedCatalogVO;
import com.yonyou.pmclouds.completedcatalog.mapper.CompletedCatalogBVOMapper;
import com.yonyou.pmclouds.completedcatalog.mapper.CompletedCatalogVOMapper;
import com.yonyou.pmclouds.completedcatalog.rmiitf.CompletedCatalogQuery;
import com.yonyou.pmclouds.completedcollate.entity.CompletedCollateVO;
import com.yonyou.pmclouds.completedcollate.mapper.CompletedCollateVOMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 竣工目录数据查询实现
 *
 * @author yzx
 */
@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = CompletedCatalogQuery.class)
public class CompletedCatalogQueryImpl implements CompletedCatalogQuery {
    @Autowired
    private CompletedCatalogVOMapper catalogVOMapper;
    @Autowired
    private CompletedCatalogBVOMapper catalogBVOMapper;
    @Autowired
    private CompletedCollateVOMapper collateVOMapper;

    @Override
    public CompletedCatalogVO getById(String id) {
        // 根据主键获取竣工资料目录主表数据
        CompletedCatalogVO completedCatalogVO = catalogVOMapper.queryByPrimaryKey(id);
        // 根据主表主键获取子表数据
        CompletedCatalogBVO[] catalogBVOS = catalogBVOMapper.queryBvosByPkCatalog(id);
        completedCatalogVO.setCompletedCatalogBVOS(catalogBVOS);
        return completedCatalogVO;
    }

    @Override
    public PageObject search(String condition, Integer start, Integer size) {
        String pkTenant = RuntimeEnvironment.getTenantId();
        // 将传入的页码转成对应数据开始行
        start = (start - 1) * size;
        List<CompletedCatalogVO> catalogVOList = new ArrayList<>();
        // 分解查询条件
        if (condition == null) {
            condition = "";
        }
        String[] conditions = condition.split(" ");
        int pageCount = catalogVOMapper.pageCount(conditions, start, size, pkTenant);
        if (pageCount > 0) {
            catalogVOList = catalogVOMapper.querypagebyconditions(conditions, start, size, pkTenant);
        }
        return new PageObject(catalogVOList.toArray(new CompletedCatalogVO[catalogVOList.size()]), pageCount, start);
    }

    /**
     * 目前该接口只根据租户查询对应的目录数据（不包含表体，如果后续有需要可以填充）
     *
     * @param pkTenantId
     * @return java.util.List<com.yonyou.pmclouds.completedcatalog.entity.CompletedCatalogVO>
     * @author yzx
     * @date 2018/5/24 11:18
     */
    @Override
    public List<CompletedCatalogVO> queryBypkTenantId(String pkTenantId) throws BusinessException {
        return catalogVOMapper.queryByPkTenant(pkTenantId);
    }

    @Override
    public boolean isExistRef(String pkCompletedCatalog) throws BusinessException {
        List<CompletedCollateVO> collateVOList = collateVOMapper.queryByCatalogPk(pkCompletedCatalog, RuntimeEnvironment.getTenantId());
        return collateVOList.size() > 0;
    }
}
